package io.github.yuegod.clean.acl.api.wechat;

import io.github.yuegod.clean.acl.api.Api;
import io.github.yuegod.clean.acl.api.ApiFactory;
import io.github.yuegod.clean.acl.api.Exec;
import io.github.yuegod.clean.acl.enumeration.Action;
import io.github.yuegod.clean.acl.enumeration.ApiType;
import io.github.yuegod.clean.acl.model.Request;
import io.github.yuegod.clean.acl.model.Result;
import io.github.yuegod.clean.acl.property.ApiAclClientProperties;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author quziwei
 * @date 2021/8/5 11:07
 **/
public abstract class WechatApiBase implements Api, InitializingBean {

    protected final Map<String, Exec> factory = new HashMap<>();

    @Resource
    protected ApiAclClientProperties apiAclClientProperties;

    @Override
    public Result doExec(Request request, Action action) {
        Exec exec = factory.get(action.name());
        return exec.accept(request);
    }

    @Override
    public void afterPropertiesSet() {
        ApiFactory.setApiFactory(ApiType.WECHAT, this);
    }

    protected void setAppId(Map<String,Object> body){
        body.put("appid",apiAclClientProperties.getWechat().getAppid());
    }

    protected void setKey(Map<String,Object> body){
        body.put("secret",apiAclClientProperties.getWechat().getKey());
    }
}
